{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Selenium\n",
    "\n",
    "## 概要\n",
    "\n",
    "* Seleniumとは\n",
    "    * Webブラウザで自動操作するためのライブラリ\n",
    "    * 人間がWebブラウザでやるような手作業を、自動的に再現\n",
    "    * 一般的な自動テストフレームワークと組み合わせることでEnd to End（E2E）テストの自動化を実現\n",
    "    \n",
    "* 歴史\n",
    "    * 2004年\n",
    "        * Selenium 1(RC)\n",
    "            * 最初のバージョン\n",
    "            * Selenium CoreがSelenium 1の中核技術\n",
    "    * 2011年\n",
    "        * Selenium 2\n",
    "            * WebDriver\n",
    "                * もうひとつの中核技術\n",
    "                * Selenium Coreと併用可能\n",
    "                * Googleにより開発される\n",
    "    * 2016年\n",
    "        * Selenium 3\n",
    "            * Selenium Coreが切り離される\n",
    "            * WebDriverにのみ対応"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ChromeDriverのインストール\n",
    "\n",
    "### Ubuntu\n",
    "\n",
    "```\n",
    "$ wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/2.34/chromedriver_linux64.zip\n",
    "$ sudo unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/\n",
    "$ sudo chmod +x /usr/local/bin/chromedriver\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## RubyでSelenium\n",
    "\n",
    "* 直接使う\n",
    "* Capybaraを利用する\n",
    "    * E2Eテストフレームワーク"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 直接使う\n",
    "\n",
    "* [RubyでSeleniumを使ってスクレイピング](https://qiita.com/tomerun/items/9cb81d7a98150ff22f53)\n",
    "\n",
    "```\n",
    "$ vi Gemfile\n",
    "================================================\n",
    "...\n",
    "gem \"selenium-webdriver\"\n",
    "================================================\n",
    "$ bundle install --path vendor/bundle\n",
    "$ vi sample.rb\n",
    "================================================\n",
    "require 'selenium-webdriver'\n",
    "\n",
    "driver = Selenium::WebDriver.for :chrome\n",
    "driver.quit\n",
    "================================================\n",
    "$ bundle exec ruby sample.rb\n",
    "```\n",
    "\n",
    "### Capybaraを利用する\n",
    "\n",
    "* [CapybaraとSeleniumを使ってGoogle検索するサンプルコード](https://qiita.com/sanryuu/items/5b1b4794f5fd3b26679c)\n",
    "* [Capybara の README 和訳](https://github.com/willnet/capybara-readme-ja)\n",
    "\n",
    "```\n",
    "$ vi Gemfile\n",
    "================================================\n",
    "...\n",
    "gem \"capybara\"\n",
    "gem \"selenium-webdriver\"\n",
    "================================================\n",
    "$ bundle install --path vendor/bundle\n",
    "$ vi sample_capybara.rb\n",
    "================================================\n",
    "require \"capybara\"\n",
    "require \"capybara/dsl\"\n",
    "require \"selenium-webdriver\"\n",
    "\n",
    "#Capybara.current_driver = :selenium\n",
    "Capybara.register_driver :selenium_chrome do |app|\n",
    "  Capybara::Selenium::Driver.new(app, :browser => :chrome)\n",
    "end\n",
    "Capybara.current_driver = :selenium_chrome\n",
    "\n",
    "\n",
    "module Crowler\n",
    "  class Google\n",
    "    include Capybara::DSL\n",
    "\n",
    "    def hit_num keyword\n",
    "\n",
    "      visit URI.escape(\"https://www.google.co.jp/search?q=\" + keyword)\n",
    "\n",
    "      result_status = all(\"#resultStats\")[0]\n",
    "      unless result_status.nil?\n",
    "        result_status.text.match(/(\\d+,)*\\d+/)[0].gsub(\",\",\"\").to_i\n",
    "      else\n",
    "        0\n",
    "      end\n",
    "    end\n",
    "\n",
    "  end\n",
    "end\n",
    "\n",
    "blowser = Crowler::Google.new\n",
    "print blowser.hit_num \"Capybara Selenium\"\n",
    "================================================\n",
    "$ bundle exec ruby sample_capybara.rb\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.3.3",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.3.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
